<?php
/**

 */
define('IN_SYS', true);
require '../framework/bootstrap.inc.php';
require IA_ROOT . '/web/common/bootstrap.sys.inc.php';
load()->web('common');
load()->web('template');

$acl = array(
    'sms' => array(
        'default' => 'sendee',
        'founder' => array()
    ),
    'plan' => array(
        'default' => 'plan',
        'founder' => array()
    ),
    'grant' => array(
        'default' => 'grant',
        'founder' => array()
    ),
    'finance' => array(
        'default' => 'finance',
        'founder' => array()
    ),


    'system' => array(
        'default' => 'welcome',
        'founder' => array()
    ),
    'account' => array(
        'default' => 'display',
        'founder' => array(
            'permission',
            'groups'
        )
    ),

    'user' => array(
        'default' => 'display',
        'direct' => array(
            'login',
            'register',
            'logout'
        ),
        'founder' => array(
            'create',
            'display',
            'edit',
            'fields',
            'group',
            'permission',
            'registerset',
        )
    ),
    'utility' => array(
        'founder' => array(
            'user'
        ),
        'direct' => array(
            'verifycode'
        )
    )
);

//检查$controller是否在允许的范围内


if(empty($controller)||!array_key_exists($controller, $acl)) {
	$controller = 'system';
}
$system='plan';
if(in_array($controller,array('plan','grant','finance'))){
    $system=$controller;
    $controller='sms';
}

$init = IA_ROOT . "/web/source/{$controller}/__init.php";
if(is_file($init)) {
	require $init;;
}

$actions = array();
$handle = opendir(IA_ROOT . '/web/source/' . $controller);
if(!empty($handle)) {
	while($dir = readdir($handle)) {
		if($dir != '.' && $dir != '..' && strexists($dir, '.ctrl.php')) {
			$dir = str_replace('.ctrl.php', '', $dir);
			$actions[] = $dir;
		}
	}
}
if(empty($actions)) {
	header('location: ?refresh');
}
if(!in_array($action, $actions)) {
	$action = $acl[$controller]['default'];
}

if(!empty($frames) && is_array($frames)) {
	foreach($frames as &$frame) {
		foreach($frame['items'] as &$fr) {
			$query = parse_url($fr['url'], PHP_URL_QUERY);
			parse_str($query, $urls);
			if(defined('ACTIVE_FRAME_URL')) {
				parse_str(ACTIVE_FRAME_URL, $get);
			} else {
				$get = $_GET;
			}
			$get['c'] = $controller;
			$get['a'] = $action;
			if(!empty($do)) {
				$get['do'] = $do;
			}
			$diff = array_diff_assoc($urls, $get);
			if(empty($diff)) {
				$fr['active'] = ' active';
			}
		}
	}
	unset($get);
	unset($query);
	unset($urls);
	unset($fr);
	unset($frame);
}


$_W['page'] = array();


if(is_array($acl[$controller]['direct']) && in_array($action, $acl[$controller]['direct'])) {
		require _forward($controller, $action);
	exit;
}
if(is_array($acl[$controller]['founder']) && in_array($action, $acl[$controller]['founder'])) {
		if(!$_W['isfounder']) {
		message('不能访问, 需要创始人权限才能访问.');
	}
}

checklogin();
require _forward($controller, $action);

function _forward($c, $a) {
	$file = IA_ROOT . '/web/source/' . $c . '/' . $a . '.ctrl.php';
	return $file;
}